//
//  STServiceListVC.m
//  YiYueHairdresser
//
//  Created by 路人甲 on 2018/10/30.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STServiceListVC.h"
#import "STServiceListView.h"
#import "STSerSetVC.h"
#import "STSingleMealVC.h"
#import "STMultiMealVC.h"


@interface STServiceListVC ()<STServiceListViewDelegate>{
    int page;
    int size;
    STServiceListView * listView;
    NSMutableArray * arrServices;
}

@end

@implementation STServiceListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor groupTableViewBackgroundColor];
    page = 1;
    size = 20;
    arrServices = [NSMutableArray arrayWithCapacity:0];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (!listView) {
        [self loadServiceListView];
        [self loadServiceListData];
    }
}

- (void)loadServiceListView {
    listView = [[STServiceListView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-35-64)];
    listView.serviceListArr = arrServices;
    listView.serviceDelegate = self;
    listView.serviceType = self.type;
    [self.view addSubview:listView];
    [listView loadServiceListView];
}

- (void)loadServiceListData {
    [STHairService getBarberServerListWithOnline:!self.type page:page completion:^(id result, NSError *error) {
        if(error){
            [ZYHCommonMethod showError:error];
        }
        if(page == 1){
             arrServices =(NSMutableArray *) [NSArray modelArrayWithClass:[STHairServiceInfoModel class] json:result];
        }else{
            [arrServices addObjectsFromArray: [NSArray modelArrayWithClass:[STHairServiceInfoModel class] json:result]];
        }
        listView.serviceListArr = arrServices;
        [listView loadServiceListView];
    }];
    
}


-(void)serviceListViewUpSlide{
    page++;
    [self loadServiceListData];
}

-(void)serviceListViewDownSlide{
    page = 1;
    [self loadServiceListData];
}

    
-(void)serviceListViewSelIndexPath:(NSIndexPath *)indexPath data:(id)data{
    
}

-(void)serviceListViewDownActionIndexPath:(NSIndexPath *)indexPath data:(id)data{
    STHairServiceInfoModel * model = arrServices[indexPath.row];
    WeakSelf
    if(self.type == 0){
        [ZYHCommonMethod showAlertActionViewWithShowVC:self showMsg:@"你确定下架该服务？" sureBtnBack:^(BOOL succeeded, NSError *error) {
            [STHairService changeBarberServerLineStatusWithServiceId:model.serviceId online:0 completion:^(id result, NSError *error) {
                if(error){
                    [ZYHCommonMethod showError:error];
                }
                [weakSelf removeCell:indexPath];
            }];
        }];
        
    }else{
        [ZYHCommonMethod showAlertActionViewWithShowVC:self showMsg:@"你确定删除该服务？" sureBtnBack:^(BOOL succeeded, NSError *error) {
            [STHairService deteleBarberServerWithServiceId:model.serviceId online:1 completion:^(id result, NSError *error) {
                if(error){
                    [ZYHCommonMethod showError:error];
                }
                [weakSelf removeCell:indexPath];
            }];
        }];
    }
        
}

-(void)serviceListViewChangeActionIndexPath:(NSIndexPath *)indexPath data:(id)data{
    STHairServiceInfoModel * model = arrServices[indexPath.section];
    WeakSelf
    if(self.type == 0){
        if(model.type == 0){
            STSerSetVC * newSer = [[STSerSetVC alloc]init];
            newSer.serName = model.serviceName;
            newSer.serType = @"Individual";
            newSer.serviceId =model.serviceId;
            newSer.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:newSer animated:NO];
        }else if(model.type == 1){
            STSingleMealVC * singleVC = [STSingleMealVC new];
            singleVC.serviceId =model.serviceId;
            singleVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:singleVC animated:NO];
        }else if(model.type == 2){
            STMultiMealVC * multiVC = [STMultiMealVC new];
            multiVC.serviceId =model.serviceId;
            multiVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:multiVC animated:NO];
        }
     }else{
         [ZYHCommonMethod showAlertActionViewWithShowVC:self showMsg:@"请确认是否上架该服务？" sureBtnBack:^(BOOL succeeded, NSError *error) {
             [STHairService changeBarberServerLineStatusWithServiceId:model.serviceId online:1 completion:^(id result, NSError *error) {
                 if(error){
                     [ZYHCommonMethod showError:error];
                 }
                 [weakSelf removeCell:indexPath];
             }];
         }];
     }
}

-(void)removeCell:(NSIndexPath*)indexParh{
    [arrServices removeObjectAtIndex:indexParh.section];
    listView.serviceListArr = arrServices;
    [listView loadServiceListView];
}



@end
